package com.why.whyoj.judge.strategy;

import com.why.whyoj.judge.codesanbox.model.ExecuteCodeResponse;
import com.why.whyoj.model.dto.question.JudgeCase;
import com.why.whyoj.model.dto.question.JudgeConfig;
import com.why.whyoj.model.dto.questionsubmit.JudgeInfo;
import com.why.whyoj.model.enums.JudgeInfoMessageEnum;

import java.util.List;

public class DefaultJudgeStrategy implements JudgeStrategy {
    @Override
    public JudgeInfo doJudge(JudgeContext context) {
        List<JudgeCase> judgeCases = context.getJudgeCases();
        JudgeConfig judgeConfig = context.getJudgeConfig();
        ExecuteCodeResponse response = context.getResponse();
        List<String> outputList = response.getOutputList();
        JudgeInfo judgeInfo = new JudgeInfo();
        judgeInfo.setMemory(response.getJudgeInfo().getMemory());
        judgeInfo.setTime(response.getJudgeInfo().getTime());
        judgeInfo.setMessage(JudgeInfoMessageEnum.ACCECPTED.getValue());
        if (outputList.size() != judgeCases.size()) {
            judgeInfo.setMessage(JudgeInfoMessageEnum.WRONG_ANSWER.getValue());
            return judgeInfo;
        }
        for (int i = 0; i < judgeCases.size(); i++) {
            if (!judgeCases.get(i).getOutput().equals(outputList.get(i))) {
                judgeInfo.setMessage(JudgeInfoMessageEnum.WRONG_ANSWER.getValue());
                return judgeInfo;
            }
        }
        int memoryLimit = judgeConfig.getMemoryLimit();
        long timeLimit = judgeConfig.getTimeLimit();
        long memory = response.getJudgeInfo().getMemory();
        long time = response.getJudgeInfo().getTime();
        if (memory > memoryLimit) {
            judgeInfo.setMessage(JudgeInfoMessageEnum.MEMORY_LIMIT_EXCEEDED.getValue());
            return judgeInfo;
        }
        if (time > timeLimit) {
            judgeInfo.setMessage(JudgeInfoMessageEnum.TIME_LIMIT_EXCEEDED.getValue());
            return judgeInfo;
        }
        judgeInfo.setMessage(JudgeInfoMessageEnum.ACCECPTED.getValue());
        judgeInfo.setMemory(memory);
        judgeInfo.setTime(time);
        return judgeInfo;
    }

}

